import 'dart:io';

import 'package:amoy_ticket/people/people_main/more_service/animation_area/animated_demo_route.dart';
import 'package:amoy_ticket/people/people_main/more_service/custom_widget/custom_widget_route.dart';
import 'package:amoy_ticket/utils/device_info_plus.dart';
import 'package:amoy_ticket/utils/router/getRouteConfig.dart';
import 'package:amoy_ticket/widget/show_text_fold.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import '../../../utils/net_utils/http_manager.dart';
import 'flow_demo/demo_flow_circle.dart';
import 'flow_demo/demo_flow_menu.dart';
import 'flow_demo/demo_flow_pop_menu.dart';
import 'page_view_page.dart';
import 'text_roll/text_roll_page.dart';

class MoreServicePage extends StatefulWidget {
  MoreServicePage({Key? key}) : super(key: key);

  @override
  State<MoreServicePage> createState() => _MoreServicePageState();
}

class _MoreServicePageState extends State<MoreServicePage>
    with SingleTickerProviderStateMixin {
  List<User> data = [
    User('影票票', 18),
    User('影票票01', 19),
    User('影票票002', 20),
    User('影票票0003', 21),
    User('影票票00004', 22),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('more service'.tr),
      ),
      body: SingleChildScrollView(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              SizedBox(height: 20),
              ElevatedButton(
                child: Text(
                  '获取设备信息',
                  style: TextStyle(fontFamily: 'hanti'),
                ),
                onPressed: () {
                  _showDeviceInfo();
                },
              ),
              ElevatedButton(
                child: Text(
                  '文本折行显示',
                  style: TextStyle(fontFamily: 'kaiti'),
                ),
                onPressed: () {
                  _showTextfold();
                },
              ),
              ElevatedButton(
                child: Text('打开webview'),
                onPressed: () {
                  Get.toNamed(RouteConfig.webviewPage, arguments: {
                    'title': 'hello',
                    'url':
                        'https://t.taopiaopiao.com/yep/page/m/zwdmslr?shared-by-app=other',
                    'isRoot': 'false',
                  });
                },
              ),
              ElevatedButton(
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.red),
                  foregroundColor: MaterialStateProperty.all(Colors.white),
                  textStyle: MaterialStateProperty.all(
                    TextStyle(fontSize: 15.sp, fontWeight: FontWeight.bold),
                  ),
                ),
                child: Text('水平展开/收起菜单'),
                onPressed: () {
                  Get.to(() => DemoFlowPopMenu());
                },
              ),
              ElevatedButton(
                child: Text('圆形展开/收起'),
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.green),
                ),
                onPressed: () {
                  Get.to(() => DemoFlowCircle());
                },
              ),
              ElevatedButton(
                child: Text('半圆菜单展开/收起'),
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.blue),
                  overlayColor: MaterialStateProperty.all(Colors.yellow),
                ),
                onPressed: () {
                  Get.to(() => DemoFlowMenu());
                },
              ),
              ElevatedButton(
                child: Text('pageView 轮播图'),
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.orange),
                  overlayColor: MaterialStateProperty.all(Colors.yellow),
                ),
                onPressed: () {},
              ),
              ViewPage(),
              Divider(),
              _dataTable(),

              ElevatedButton(
                child: Text('animated demo route '),
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.orange),
                  overlayColor:
                      MaterialStateProperty.all(Color.fromARGB(255, 17, 16, 6)),
                ),
                onPressed: () => Get.to(() => AnimatedDemoRoute()),
              ),

              ElevatedButton(
                child: Text('自定义组件'),
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.orange),
                  overlayColor:
                      MaterialStateProperty.all(Color.fromARGB(255, 17, 16, 6)),
                ),
                onPressed: () => Get.to(() => CustomWidgetRoute()),
              ),

              ///文字渐变色
              _textGradientColor(),

              ///歌词滚动
              ElevatedButton(
                child: Text('音乐播放歌词滚动'),
                style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.orange),
                ),
                onPressed: () => Get.to(() => TextRollPage()),
              ),

              //接口调用
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 20, vertical: 30.w),
                child: ElevatedButton(
                  child: Text('高德地图api调用'),
                  style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all(Colors.orange),
                  ),
                  onPressed: () => _getData(),
                ),
              ),
              //接口调用
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 20, vertical: 30.w),
                child: ElevatedButton(
                  child: Text('高德地图api调用 天气查询 ------http manager'),
                  style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all(Colors.orange),
                  ),
                  onPressed: () => _getDataUseHttpManager(),
                ),
              ),
              //接口调用
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 20, vertical: 30.w),
                child: ElevatedButton(
                  child: Text('高德地图api调用 输入提示------http manager'),
                  style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.all(Colors.orange),
                  ),
                  onPressed: () => _getinPutTips(),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Future<void> _getinPutTips() async {
    try {
      var response = await HttpManager.getInstance().get(
        '/v3/assistant/inputtips',
        params: {
          'keywords': '肯德基',
          'type': '050301',
          'location': '116.481488,39.990464',
          'city': '北京',
          'datatype': 'all',
          'key': '6365c6cd66ce60460e065dcfdb3e0a18',
        },
        withLoading: true,
      );

      SmartDialog.showToast(
        response.toString(),
      );

      print(response.toString());
    } catch (e) {
      print(e);
    }
  }

  Future<void> _getDataUseHttpManager() async {
    try {
      var response = await HttpManager.getInstance().get(
        '/v3/weather/weatherInfo',
        params: {
          'city': '110101',
          'extensions': 'base',
          'key': '6365c6cd66ce60460e065dcfdb3e0a18',
        },
        withLoading: true,
      );
      await SmartDialog.showToast(
        response.toString(),
        alignment: Alignment.center,
        displayTime: Duration(seconds: 5),
        clickMaskDismiss: true,
      );
    } catch (e) {
      print(e);
    }
  }

  //获取数据
  Future<void> _getData() async {
    try {
      Map _resp = await HttpManager.getInstance().get(
        '/v3/weather/weatherInfo',
        params: {
          'city': '110101',
          'extensions': 'base',
          'key': '6365c6cd66ce60460e065dcfdb3e0a18',
        },
      );
      await SmartDialog.showToast(
        _resp.mapToStructureString(),
        alignment: Alignment.center,
        displayTime: Duration(seconds: 5),
        clickMaskDismiss: true,
        usePenetrate: false,
      );
    } catch (e) {
      print(e);
    }
  }

  //文字变色
  _textGradientColor() {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 30.w, vertical: 50.w),
      child: ShaderMask(
        shaderCallback: (Rect bounds) {
          return LinearGradient(
            colors: <Color>[Colors.blue, Colors.red],
            tileMode: TileMode.clamp,
          ).createShader(bounds);
        },
        blendMode: BlendMode.srcATop,
        child: Center(
          child: Text(
            '我再次看到日出，并非是早起，而是熬了整个通宵，绝美的晨光此刻你不在我的身旁',
            style: TextStyle(fontSize: 15),
            overflow: TextOverflow.clip,
          ),
        ),
      ),
    );
  }

  //展示设备信息
  _showDeviceInfo() async {
    var deviceInfo = await getDeviceInfo();
    var platformId = await FlutterDeviceInfo.platformUid;
    var platformName = await FlutterDeviceInfo.platformName;
    var platformModel = await FlutterDeviceInfo.platformModel;
    SmartDialog.compatible.show(
      widget: Container(
        padding: EdgeInsets.all(20),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(20),
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: Platform.isAndroid
              ? [
                  Text('Android设备信息:', style: TextStyle(color: Colors.red)),
                  Text('设备唯一标识:$platformId'),
                  Text('设备名称:$platformName'),
                  Text('设备model:$platformModel'),
                  Text('手机系统版本号:${deviceInfo.version.incremental}'),
                  Text('手机的唯一设备号:${deviceInfo.id}'),
                  Text('是否是真机:${deviceInfo.isPhysicalDevice}'),
                  Text('手机的CPU型号:${deviceInfo.supportedAbis}'),
                ]
              : Platform.isIOS
                  ? [
                      Text('iOS设备信息:', style: TextStyle(color: Colors.red)),
                      Text('设备唯一标识:$platformId'),
                      Text('设备名称:$platformName'),
                      Text('设备model:$platformModel'),
                      Text('手机系统的版本号:${deviceInfo.systemVersion}'),
                      Text('手机的唯一设备号:${deviceInfo.identifierForVendor}'),
                      Text('是否是真机:${deviceInfo.isPhysicalDevice}'),
                      Text('手机的cpu型号${deviceInfo.utsname.machine}'),
                    ]
                  : [Text('其他机型暂不支持~~~~')],
        ),
      ),
    );
  }

  // 文本折行显示
  _showTextfold() {
    SmartDialog.compatible.show(
      widget: Container(
        padding: EdgeInsets.all(20),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(20),
        ),
        width: 200,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            ShowTextFold(
                maxLine: 3,
                text: 'skssslmslmsklsmslm klsmklsmkslmkslmskmsklsmkdm'),
            Divider(thickness: 1),
            ShowTextFold(
                maxLine: 3,
                text:
                    'hel29292233nksskssmskskssljslsnknsmkssmklmdklmdldlhsl29292233nksskssmsljnhel29292233nksskssmsl'),
            Divider(thickness: 1),
            ShowTextFold(maxLine: 3, text: '上课什么是快乐秘书科秘书看来是是科目三考试模块什么是快乐秘书科秘书'),
            Divider(thickness: 1),
            ShowTextFold(
                maxLine: 3,
                text:
                    '实际上是是可怜见看来是什么马克思莫斯科老师开始了马克思没啥可买的思考什么款式龙门石窟六十米宽六十米宽鲁克山脉上空马赛克了'),
          ],
        ),
      ),
    );
  }

  _dataTable() {
    List<DataRow> dateRows = [];
    for (int i = 0; i < data.length; i++) {
      dateRows.add(DataRow(
        selected: data[i].isSelect,
        onSelectChanged: ((selected) {
          setState(() {
            data[i].isSelect = selected!;
          });
          print('$i的${data[i].isSelect}');
          // SmartDialog.showToast(
          //   '点击了第$i行',
          //   debounce: true,
          //   usePenetrate: false,
          // );
        }),
        cells: [
          DataCell(Text('${data[i].name}')),
          DataCell(Text('${data[i].age}')),
          DataCell(Text('男')),
          DataCell(Text('2020')),
          DataCell(Text('10')),
        ],
      ));
    }
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      child: Container(
        decoration: BoxDecoration(border: Border.all(color: Colors.red)),
        padding: EdgeInsets.all(20.w),
        child: DataTable(
          sortColumnIndex: 1,
          sortAscending: true,
          columns: [
            DataColumn(
                label: Text(
                  '姓名',
                  style: TextStyle(fontFamily: 'kaiti'),
                ),
                numeric: true),
            DataColumn(label: Text('年龄'), tooltip: '年龄'),
            DataColumn(
                label: Text('性别'),
                onSort: (i, b) {
                  SmartDialog.showToast('性别');
                }),
            DataColumn(
              label: Text('出生年份'),
            ),
            DataColumn(
              label: Text('出生月份'),
            ),
          ],
          rows: dateRows,
        ),
      ),
    );
  }
}

class User {
  User(this.name, this.age, {this.isSelect = false});

  final String name;
  final int age;
  bool isSelect;
}
